package mpcc.mpcc.activity.busi_presentation;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import mpcc.mpcc.activity.MPCCActivity;
import mpcc.mpcc.activity.R;
import mpcc.mpcc.base.BaseActivity;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class NewsListActivity extends BaseActivity {
	private List<Map<String, Object>> mData;
	private ListView newsListView;
	private Button newsListReturn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.newslist);
		newsListView = (ListView) findViewById(R.id.newsListView);
		newsListReturn = (Button) findViewById(R.id.newsListReturn);
		generateData();
		MyAdapter adapter = new MyAdapter(this);
		newsListView.setAdapter(adapter);
		newsListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int position,
					long id) {
				String newsid = (String) mData.get(position).get("newsid");
				showInfo(newsid);
			}
		});
		addChangeActListener(newsListReturn, NewsListActivity.this,
				AllBusinessActivity.class);

	}

	public void generateData() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String requestStr = "10100001" + sdf.format(new Date());
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("title", "1");
		map.put("newsid", "1");
		list.add(map);
		map = new HashMap<String, Object>();
		map.put("title", "2");
		map.put("newsid", "2");
		list.add(map);
		map = new HashMap<String, Object>();
		map.put("title", "3");
		map.put("newsid", "3");
		list.add(map);
		setmData(list);
	}

	public void setmData(List<Map<String, Object>> mData) {
		this.mData = mData;
	}

	public void showInfo(String newsid) {

		ArrayList<String> arrClass = new ArrayList<String>();
		arrClass.add(newsid);
		Bundle bundle = new Bundle();
		bundle.putSerializable("newsidlist", arrClass);

		Intent intent = new Intent();
		intent.putExtras(bundle);

		intent.setClass(this, NewsShowActivity.class);
		NewsListActivity.this.startActivity(intent);
	}

	public final class ViewHolder {
		public TextView title;
	}

	public class MyAdapter extends BaseAdapter {

		private LayoutInflater mInflater;

		public MyAdapter(Context context) {
			this.mInflater = LayoutInflater.from(context);
		}

		public int getCount() {
			return mData.size();
		}

		public Object getItem(int arg0) {
			return null;
		}

		public long getItemId(int arg0) {
			return 0;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = mInflater.inflate(R.layout.vlist, null);
				holder.title = (TextView) convertView.findViewById(R.id.title);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.title.setText((String) mData.get(position).get("title"));

			return convertView;
		}
	}
}
